package com.qfedu.bookstores.controller;

import com.qfedu.bookstores.entity.Book;
import com.qfedu.bookstores.service.IBookService;
import com.qfedu.bookstores.service.impl.IBookServiceImpl;
import com.qfedu.bookstores.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/manager/bookServlet")
public class BookServlet extends BaseServlet {
     //创建一个图书Service对象
    private IBookService bookService = new IBookServiceImpl();

    /**
     * 图书页面展示数据使用
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        request.setAttribute("books",books);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").
                forward(request, response);
    }
    /**
     * 图书页面添加数据使用
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取book_edit.jsp页面中表单数据值并存储在Book实体类对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        //2.调用业务层逻辑触发dao层操作保存数据
        bookService.addBook(book);
        /**
         * 表单提交中隐藏问题 --> 表单数据重复提交
         * 当用户提交完亲切感会之后,浏览器会记录下最后一次请求全部数据信息,当用户进行页面刷新的时候
         * 浏览器就会记录最后一次请求并再次提交数据  --> 表单数据重复提交
         * 为了避免着这样表单重复提交数据出现,建议在页面条件的操作以"从重定向的方式来完成"
         * 重定向的效果是两次请求,第二次request对象是不会记录第一次request数据
         */
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=list");
    }
    /**
     * 图书页面删除数据使用
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取id 参数
        String id = request.getParameter("id");
        //2.调用service触发dao层调用完成对图书删除操作
        bookService.deleteBookById(Integer.valueOf(id));
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=list");
    }
    /**
     * 查询图书数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取id 参数
        String id = request.getParameter("id");
        //2.调用service触发dao层调用完成对图书查询操作
        Book book = bookService.queryBookById(Integer.valueOf(id));
        //3.将book对象存在request作用域中
        request.setAttribute("book", book);
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").
                forward(request, response);
    }
    /**
     * 图书修改数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.使用BeanUtils注入将数据存在book实体类对象中
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        //2.代用service层触发dao层中修改数据操作
        bookService.updateBook(book);
        //3.重定向进行数据展示
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=list");
    }
}
